package com.jushu.storbox.task;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.SharedUser;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.modelV2.request.ShareShipsRequestV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.INodeShareV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.ShareResourceV2;
import com.huawei.sharedrive.sdk.android.serviceV2.ShareClientV2;

public class GetShareUserAsyncTask extends AsyncTask<FileFolderInfo, Integer, List<INodeShareV2>> {
	
	private static final String LOG_TAG = GetShareUserAsyncTask.class.getSimpleName();

	private FileFolderInfo fileFolderInfomation = null;
	
	private Handler resultHandlder = null;
	
	public GetShareUserAsyncTask(FileFolderInfo fileFolderInfo, Handler resultHandlder) {
		if(fileFolderInfo == null) {
			throw new IllegalArgumentException("FileFolderInfo instance is null");
		}
		this.fileFolderInfomation = fileFolderInfo;
		
		this.resultHandlder = resultHandlder;
	}

	@Override
	protected List<INodeShareV2> doInBackground(FileFolderInfo... arg0) {
		List<INodeShareV2> listSharedUser = null;
		try {
			LogUtil.i(LOG_TAG, "get list share user data...");
			ShareShipsRequestV2 request = new ShareShipsRequestV2();
			request.setOwnerID(ShareDriveApplication.getInstance()
					.getWnerID());
			request.setNodeId(fileFolderInfomation.getId());
			ShareResourceV2 response = ShareClientV2.getInstance().listShareShips(
					ShareDriveApplication.getInstance().getAuthorization(),
					request);
			listSharedUser = response.getContents();
		} catch (ClientException e) {
			LogUtil.e(LOG_TAG, "[listSharedUser]code:" + e.getCode()
					+ " | message:" + e.getLocalizedMessage());
			if(resultHandlder != null) {
				Message msg = resultHandlder.obtainMessage(ClientExceptionRelateHandler.CLIENT_EXCEPTION_OCCURED, e);
				msg.sendToTarget();
			} else {
				LogUtil.e(LOG_TAG, "resultHandlder is null");
			}
		}
		return listSharedUser;
	}

	@Override
	protected void onPostExecute(List<INodeShareV2> SharedUserList) {
		if (SharedUserList != null) {
			List<SharedUser> shareADuserList = new ArrayList<SharedUser>();
			for (INodeShareV2 inodeShare : SharedUserList) {
				SharedUser sharedUser = new SharedUser();
				sharedUser.setShareStruts(true);
				sharedUser.setiNodeId(inodeShare.getNodeId());
				sharedUser.setModifiedAt(inodeShare.getModifiedAt());
				sharedUser.setName(inodeShare.getName());
				sharedUser.setSharedDepartment(inodeShare
						.getSharedUserDescription());
				sharedUser.setSharedUserLoginName(inodeShare
						.getSharedUserLoginName());
				sharedUser.setSharedUserName(inodeShare
						.getSharedUserName());
				sharedUser
						.setSharedUserId(inodeShare.getSharedUserId());
				shareADuserList.add(sharedUser);
			}
			if(resultHandlder != null) {
				Message msg = resultHandlder.obtainMessage(UiConstant.SUCCESS_GET_SHARED_USER_FOR_FILEFOLDER,
						shareADuserList);
				msg.sendToTarget();
			} else {
				LogUtil.e(LOG_TAG, "resultHandlder is null");
			}
		}
	}
	
}
